package com.example.ceramics.activity;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.ceramics.R;
import com.example.ceramics.adapter.MyPagerAdapter;
import com.example.ceramics.database.dao.GoodsDaoImpl;
import com.example.ceramics.database.dao.IGoodsDao;
import com.example.ceramics.entity.TabEntity;
//import com.example.ceramics.fragment.BlankFragment;
import com.example.ceramics.fragment.CartFragment;
import com.example.ceramics.fragment.GoodsFragment;
import com.example.ceramics.fragment.HomeFragment;
import com.example.ceramics.fragment.MineFragment;
//import com.example.ceramics.fragment.PutFragment;
import com.example.ceramics.fragment.SortFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity {

    private String[] mTitles = {"首页", "分类", "购物车", "我的"/*,"测试"*/};
    private int[] mIconUnselectIds = {
            R.mipmap.home, R.mipmap.sort,
            R.mipmap.cart, R.mipmap.mine/*, R.mipmap.put,*/};
    private int[] mIconSelectIds = {
            R.mipmap.home_selected, R.mipmap.sort_selected,
            R.mipmap.cart_selected, R.mipmap.mine_selected/*, R.mipmap.put,*/};

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;

    private HomeFragment homeFragment = HomeFragment.newInstance();
    private SortFragment sortFragment = SortFragment.newInstance();
    private CartFragment cartFragment = CartFragment.newInstance();
    private MineFragment mineFragment = MineFragment.newInstance();

    private static final String Cart_FRAGMENT_KEY = "CartFragment";

    private MyPagerAdapter pagerAdapter;

    private IGoodsDao goodsDao;

//    private Button refresh;

//    private boolean isNull = true;

    public ViewPager getViewPager() {
        return viewPager;
    }

    public CommonTabLayout getCommonTabLayout() {
        return commonTabLayout;
    }

    private int tab;

    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.viewpager);
        commonTabLayout = findViewById(R.id.commonTabLayout);
//        refresh = findViewById(R.id.refresh);
    }

    @Override
    protected void initData() {
        goodsDao = new GoodsDaoImpl(this);
        if (goodsDao.isEmpty()) {
            goodsDao.addAllGoods();
        }


//        refresh.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                reLoadFragView();
//            }
//        });

        //fragment添加到viewPage中
        mFragments.add(homeFragment);
        mFragments.add(sortFragment);
        mFragments.add(cartFragment);
        mFragments.add(mineFragment);
//        mFragments.add(BlankFragment.newInstance("",""));
        //添加按钮实体
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        //将实体集合绑定到组件
        commonTabLayout.setTabData(mTabEntities);
        //Tab切换点击事件
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                if (position == 1) {
//                    navigateTo(PutActivity.class);
//                } else {
//                    viewPager.setCurrentItem(position);
//                }
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        //预加载Fragment
        viewPager.setOffscreenPageLimit(mFragments.size());
        //页面切换，tab也切换
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                if (position == 1) {
//                    navigateTo(PutActivity.class);
//                } else {
//                    commonTabLayout.setCurrentTab(position);
//                }
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mTitles, mFragments);
        viewPager.setAdapter(pagerAdapter);

//        tab = (int) getIntent().getSerializableExtra("tab");
//        viewPager.setCurrentItem(tab);
//        commonTabLayout.setCurrentTab(tab);

    }

    /*重新加载布局*/
    public void reLoadFragView() {
        pagerAdapter.removeFragment(2);
        getSupportFragmentManager().beginTransaction().remove(cartFragment).commit();

        GoodsFragment goodsFragment = GoodsFragment.newInstance("十二生肖系列");
        pagerAdapter.addFragment(2, goodsFragment);
        getSupportFragmentManager().beginTransaction().add(R.id.viewpager, goodsFragment).commit();
        pagerAdapter.notifyDataSetChanged();
    }
}